package cn.jiyun.order.service.impl;

import cn.jiyun.order.common.R;
import cn.jiyun.order.dao.OrderDao;
import cn.jiyun.order.feign.GoodsFeign;
import cn.jiyun.order.pojo.Goods;
import cn.jiyun.order.pojo.Order;
import cn.jiyun.order.pojo.dto.OrderDto;
import cn.jiyun.order.service.OrderService;
import cn.jiyun.order.utils.ExcelUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private GoodsFeign goodsFeign;
    @Override
    public R findAll(Integer page,Integer pageSize) {
        Page<Order> orderPage = new Page<>(page, pageSize);
        List<Order> orders = orderDao.selectList(null);
        List<Goods> goods = goodsFeign.findAll();
        log.info("goods:{}",goods);
        log.info("orders:{}",orders);
        ArrayList<OrderDto> orderDtoLists = new ArrayList<>();
        for (Order order : orders) {
            OrderDto orderDto = new OrderDto();
            Integer sid = order.getSid();
            for (Goods good : goods) {
                Integer id = good.getId();
                if(sid.intValue()==id.intValue()){
                    orderDto.setSname(good.getName());
                }
            }
            orderDto.setId(order.getId());
            orderDto.setAddress(order.getAddress());
            orderDto.setPrice(order.getPrice());
            orderDtoLists.add(orderDto);
        }
        log.info("orderDtos:{}",orderDtoLists);
        return R.success(orderDtoLists);
    }

    @Override
    public void exportExcel(HttpServletResponse response) {
        String[] tou={"订单编号","订单金额","收货地址","商品名称"};
        String[] ziduan={"id","price","address","sname"};
        List<Order> orders = orderDao.selectList(null);
        List<Goods> goods = goodsFeign.findAll();
        log.info("goods:{}",goods);
        log.info("orders:{}",orders);
        OrderDto orderDto = new OrderDto();
        ArrayList<OrderDto> orderDtoLists = new ArrayList<>();
        for (Order order : orders) {
            Integer sid = order.getSid();
            for (Goods good : goods) {
                Integer id = good.getId();
                if(sid==id){
                    orderDto.setSname(good.getName());
                }
            }
            orderDto.setId(order.getId());
            orderDto.setAddress(order.getAddress());
            orderDto.setPrice(order.getPrice());
            orderDtoLists.add(orderDto);
        }
        if(CollectionUtils.isEmpty(orderDtoLists)){
            log.info("集合为空:{}",orderDtoLists.toArray());
        }
        LinkedHashMap<String, String> map = new LinkedHashMap<>();
        for (int i = 0; i < ziduan.length; i++) {
            map.put(ziduan[i],"");
        }
        ExcelUtil.exportExcel(orderDtoLists.size(),response,orderDtoLists,tou,map);
    }
}
